﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CanSWAT.SWAT.Output
{
    public partial class ResultAnalysisFrm : Form
    {
        public ResultAnalysisFrm()
        {
            InitializeComponent();
        }

        public string OutputDatabase
        {
            set
            {
                //_db = new global::CanSWAT.SWAT.Output.SWATOutputDatabase(@"E:\AAFC\LaSalleArcswat2013-05-01\Lasalle-new\Scenarios\Default\TablesOut\SWATOutput.mdb");//for home
                //_db = new SWAT.Output.SWATOutputDatabase(@"C:\Users\zyu\Downloads\LaSalleArcswat2013-05-01\Lasalle-new\Scenarios\Default\TablesOut\SWATOutput_Monthly.mdb");//for university
                //_db = new SWAT.Output.SWATOutputDatabase(@"C:\zhiqiang\Existing SWAT Model\Lasalle-new\Scenarios\Default\TablesOut\SWATOutput.mdb");//for AAFC
                if (!System.IO.File.Exists(value)) return;

                _db = new SWATOutputDatabase(value);

                cmbType.DataSource = _db.Tables;
                if (_db.Tables.Count > 0)
                {
                    int index = _db.Tables.IndexOf("rch");
                    if (index == -1) index = _db.Tables.IndexOf("sub");
                    if (index > -1)
                        cmbType.SelectedIndex = index;
                }  
            }
        }

        SWAT.Output.SWATOutputDatabase _db = null;
        SWAT.Output.SWATOutputForOneType _output = null;
        OutputDisplayChart _chart = null;

        private void ResultAnalysisFrm_Load(object sender, EventArgs e)
        {
            _chart = new OutputDisplayChart();
            _chart.Dock = DockStyle.Fill;
            groupBox1.Controls.Add(_chart);           

            cmbType.SelectedIndexChanged += new EventHandler(updateType);
            updateType(null, null);
        }

        private void updateType(object sender, EventArgs e)
        {
            if (_db == null) return;
            if (cmbType.SelectedIndex < 0) return;

            cmbIDs.SelectedIndexChanged -= new EventHandler(updatePlot);
            cmbYears.SelectedIndexChanged-= new EventHandler(updatePlot);
            cmbColumns.SelectedIndexChanged -= new EventHandler(updatePlot);

            _output = _db.getOutput(cmbType.SelectedItem.ToString());
            if (_output == null) return;

            cmbIDs.DataSource = _output.IDs;
            cmbYears.DataSource = _output.Years;

            cmbColumns.DataSource = _output.Columns;
            string[] cols = new string[_output.Columns.Count];
            _output.Columns.CopyTo(cols, 0);
            cmbColumns2.DataSource = cols;

            if (_output.Columns.Count > 0)
            {
                int index = _output.Columns.IndexOf("FLOW_INcms");
                if (index == -1) index = _output.Columns.IndexOf("PRECIPmm");
                if (index > -1)
                    cmbColumns.SelectedIndex = index;
            } 

            cmbIDs.SelectedIndexChanged += new EventHandler(updatePlot);
            cmbYears.SelectedIndexChanged += new EventHandler(updatePlot);
            cmbColumns.SelectedIndexChanged += new EventHandler(updatePlot);
            cmbColumns2.SelectedIndexChanged += new EventHandler(updatePlot);
            updatePlot(null, null);
        }

        private void updatePlot(object sender, EventArgs e)
        {
            if (_chart == null) return;
            if (_output == null) return;
            if (cmbIDs.Items.Count == 0) return;
            if (cmbYears.Items.Count == 0) return;

            string column = cmbColumns.SelectedItem.ToString();
            string column2 = cmbColumns2.SelectedItem.ToString();
            int id = int.Parse(cmbIDs.SelectedItem.ToString());
            int year = int.Parse(cmbYears.SelectedItem.ToString());

            StringCollection cols = new StringCollection();
            cols.Add(column);
            cols.Add(column2);

            _chart.DrawGraph(
                _output.Plot(id, year, column),
                SWAT.Output.SWATOutputDatabase.COLUMN_NAME_DATE, cols, _output.TimeInterval);
        }

        
    }
}
